package com.rslai.commons.http.tools;

import org.apache.http.HttpResponse;
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
import org.apache.http.protocol.HttpContext;

/**
 * 连接保活策略
 * Created by lrs on 2018/8/6.
 */
public class ConnectionKeepAliveStrategy extends DefaultConnectionKeepAliveStrategy {
	
	private final long time; // KeepAlive 默认时间

    /**
     * 构造方法，如果没拿到返回这个默认时间
     * @param time 默认存活时间
     */
	public ConnectionKeepAliveStrategy(long time){
		this.time = time;
	}

    /**
     * 返回此连接的 KeepAlive 持续时间
     * @param response 返回的 response
     * @param context 正在使用上下文
     * @return 返回 KeepAlive 持续时间（ms）
     */
	@Override
	public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
		long ka = super.getKeepAliveDuration(response, context);
		if (ka == -1) {
            ka = time;
		}
		return ka;
	}

}
